<?php
/**
 * Skyline :: Patterns & component framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Skyline\Structure;

use InvalidArgumentException;
use Traversable;
use Skyline\Predicate\PredicateInterface;

/**
 * Abstract implementation of CollectionInterface that implements the
 * basic multiple element operations
 * 
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
abstract class AbstractCollection extends AbstractContainer implements CollectionInterface
{
	/**
	 * @var PredicateInterface
	 */
	protected $predicate;
	
	/**
	 * Class constructor
	 * 
	 * @param Traversable $elements [optional]
	 * @return void
	 */
	public function __construct($elements = null)
	{
		if(!is_null($elements)) {
			$this->addAll($elements);
		}
	}
	
	/**
	 * Adds multiple elements to the collection
	 * 
	 * @param Traversable $elements
	 * @return void
	 */
	public function addAll($elements)
	{
		$this->assertIsTraversable($elements);
		
		foreach($elements as $element) {
			$this->add($element);
		}
	}
	
	/**
	 * Returns true if all given elements exist in the collection
	 * 
	 * @param Traversable $elements
	 * @return bool
	 */
	public function containsAll($elements)
	{
		$this->assertIsTraversable($elements);
		
		foreach($elements as $element) {
			if(!$this->contains($element)) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Returns true if any given elements exist in the collection
	 * 
	 * @param Traversable $elements
	 * @return bool
	 */
	public function containsAny($elements)
	{
		$this->assertIsTraversable($elements);
		
		foreach($elements as $element) {
			if($this->contains($element)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Removes multiple elements from the collection
	 * 
	 * @param Traversable $elements
	 * @return void
	 */
	public function removeAll($elements)
	{
		$this->assertIsTraversable($elements);
		
		foreach($elements as $element) {
			$this->remove($element);
		}
	}
	
	/**
	 * Sets the predicate used to validate this collection
	 * 
	 * @param PredicateInterface $predicate
	 * @return void
	 */
	public function setPredicate(PredicateInterface $predicate)
	{
		$this->predicate = $predicate;
	}
	
	/**
	 * @throws InvalidArgumentException If predicate evaluates element to false
	 * @param mixed $element
	 * @return void
	 */
	protected function assertElementIsValid($element)
	{
		if(!is_null($this->predicate) && !$this->predicate->evaluate($element)) {
			throw new InvalidArgumentException('Element not valid, per predicate');
		}
	}
	
	/**
	 * @throws InvalidArgumentException If argument isn't an array or a traversable object
	 * @param Traversable $elements
	 * @return void
	 */
	protected function assertIsTraversable($elements)
	{
		if(!is_array($elements) && !($elements instanceof Traversable)) {
			throw new InvalidArgumentException('Expected traversable object or array');
		}
	}
}